package com.dlry.web.exceptionhandler;

import java.io.IOException;
import java.text.ParseException;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class CommonExceptionHandler {

	private static final Logger LOGGER = LogManager.getFormatterLogger(CommonExceptionHandler.class.getName());

	@ExceptionHandler(value = { IllegalArgumentException.class })
	public String handlerIllegalException(Exception excep, HttpServletRequest request, Model model) {
		request.setAttribute("error", excep);
		request.setAttribute("errorMessage", "参数不合法！");
		LOGGER.error(excep.getClass().getSimpleName() +  excep.getMessage());
		return "error_jsp";
	}

	@ExceptionHandler(value = { ParseException.class, IOException.class })
	public String handlerTravelErrors(Exception excep, HttpServletRequest request, Model model) {
		request.setAttribute("error", excep);
		request.setAttribute("errorMessage", "增加出差记录错误！");
		LOGGER.error(excep.getClass().getSimpleName() +  excep.getMessage());
		return "error_jsp";
	}

	@ExceptionHandler()
	public String handlerCommonErrors(Exception excep, HttpServletRequest request, Model model) {
		request.setAttribute("error", excep);
		request.setAttribute("errorMessage", "服务器未知错误！");
		LOGGER.error(excep.getClass().getSimpleName() +  excep.getMessage());
		return "error_jsp";
	}
}
